package com.limedia.explorer.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.limedia.explorer.enums.TypeHttpRequest;
import com.squareup.okhttp.MediaType;

public class HttpParams {
	
	private static final String TYPE_NAME_TEXT_PLAIN = "text/plain";
	private static final String TYPE_NAME_VIDEO_MPEG4 = "video/mpeg4";
	private static final String TYPE_NAME_JSON = "application/json;charset=utf-8";
	public static final String HTTP_HEADER_SET_COOKIE = "Set-Cookie";
	public static final String HTTP_HEADER_COOKIE = "Cookie";
	private List<Param> params = new ArrayList<Param>();
	
	public HttpParams() {
		super();
	}
	
	public void addParam(String key, TypeHttpRequest type, String value) {
		this.params.add(new Param(key, type, value));
	}

	public HashMap<String, String> getKeyValueMap() {
		HashMap<String, String> map = new HashMap<String, String>();
		for(Param param : this.params) {
			map.put(param.getKey(), param.getValue().toString());
		}
		return map;
	}
	
	public List<Param> getParams() {
		return params;
	}

	public String toString() {
		if(null != this.params) {
			String string = "";
			for(Param param : this.params) {
				string += param.getKey() + " is " + param.getValue().toString() + "\n";
			}
			return string;
		}
		return null;
	}
	
	public class Param {
		
		private String key = null;
		private String value = null;
		private MediaType type = null;
		
		public Param(String key, TypeHttpRequest type, String value) {
			super();
			
			this.key = key;
			this.value = value;
			this.type = this.parseType(type);
		}
		
		public String getKey() {
			return key;
		}

		public String getValue() {
			return value;
		}
		
		public MediaType getType() {
			return type;
		}

		private MediaType parseType(TypeHttpRequest type) {
			switch (type) {
			case TYPE_TEXT_PLAIN:
				return MediaType.parse(TYPE_NAME_TEXT_PLAIN);
			case TYPE_VIDEO_MPEG4:
				return MediaType.parse(TYPE_NAME_VIDEO_MPEG4);
			case TYPE_JSON:
				return MediaType.parse(TYPE_NAME_JSON);
			default:
				break;
			}
			return null;
		}
	}
}
